/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.GregorianCalendar;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.AccountingDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.AccountingTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.AccountingServiceInt;

/**
 * 
 * @author pkendzo
 */
@Stateless
@Remote({ AccountingServiceInt.class })
public class AccountingService implements AccountingServiceInt {

	@EJB
	private AccountingDAOInt accDao;

	private AccountingTBL tmpAcc;

	public AccountingService() {
		tmpAcc = new AccountingTBL();
	}

	@Override
	public int changePassword(String login, String newPwd) {
		AccountingTBL tmpAcc_1 = this.accDao.findAccountByLogin(login);

		tmpAcc_1.setPassword(newPwd);
		this.accDao.update(tmpAcc_1);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public String checkLogin(AccountingTBL user) {
		AccountingTBL loginUser = this.accDao.getUser(user.getLogin());
		if (loginUser != null) {
			if (loginUser.getPassword().equals(user.getPassword())) {
				tmpAcc = loginUser;
				return "loginSuccess";
			}
		}

		return "loginFailed";
	}

	/*
	 * @Override public int createAccount(String login, String defaultPwd) {
	 * AccountingTBL tmpAcc_1 = new AccountingTBL();
	 * 
	 * tmpAcc_1.setLogin(login);
	 * tmpAcc_1.setPassword(Util.createPasswordHash("MD5", Util.BASE64_ENCODING,
	 * null, login, defaultPwd));
	 * 
	 * this.accDao.persist(tmpAcc_1);
	 * 
	 * return RET_OPERATION_DONE_PROPERLY; }
	 */

	@Override
	public AccountingTBL[] showsAvailableUsers() {
		Object[] tmp = this.accDao.retrieveAvailableUsers().toArray();
		AccountingTBL[] eintraege = new AccountingTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (AccountingTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public int createAccount(String login, String defaultPwd,
			boolean Role_CustomerTAB, boolean Role_BillsTAB,
			boolean Role_OPBillsTAB, boolean Role_RemindersTAB,
			boolean Role_ZytologyTAB, boolean Role_OrdonanceTAB,
			boolean Role_DoctorLetterTAB, boolean Role_CalendarTAB,
			boolean Role_GravTestTAB, boolean Role_LaborbookTAB,
			boolean Role_InternAnaTAB, boolean Role_DeliveriesTAB,
			boolean Admin, boolean Role_tarife, boolean Role_DoctorsDB,
			boolean Role_ZytoZettelTAB) {

		AccountingTBL tmpAcc_1 = new AccountingTBL();

		tmpAcc_1.setLogin(login);
		// tmpAcc_1.setPassword(Util.createPasswordHash("MD5",
		// Util.BASE64_ENCODING, null, login, defaultPwd));
		tmpAcc_1.setPassword(defaultPwd);
		tmpAcc_1.setAdmin(Admin);
		tmpAcc_1.setRole_BillsTAB(Role_BillsTAB);
		tmpAcc_1.setRole_CustomerTAB(Role_CustomerTAB);
		tmpAcc_1.setRole_CalendarTAB(Role_CalendarTAB);
		tmpAcc_1.setRole_DeliveriesTAB(Role_DeliveriesTAB);
		tmpAcc_1.setRole_DoctorLetterTAB(Role_DoctorLetterTAB);
		tmpAcc_1.setRole_DoctorsDB(Role_DoctorsDB);
		tmpAcc_1.setRole_GravTestTAB(Role_GravTestTAB);
		tmpAcc_1.setRole_InternAnaTAB(Role_InternAnaTAB);
		tmpAcc_1.setRole_LaborbookTAB(Role_LaborbookTAB);
		tmpAcc_1.setRole_OPBillsTAB(Role_OPBillsTAB);
		tmpAcc_1.setRole_OrdonanceTAB(Role_OrdonanceTAB);
		tmpAcc_1.setRole_RemindersTAB(Role_RemindersTAB);
		tmpAcc_1.setRole_tarife(Role_tarife);
		tmpAcc_1.setRole_ZytologyTAB(Role_ZytologyTAB);
		tmpAcc_1.setRole_ZytoZettelTAB(Role_ZytoZettelTAB);

		this.accDao.persist(tmpAcc_1);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int deleteAccount(String login) {

		this.accDao.delete(login);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public AccountingTBL getAccountByLogin(AccountingTBL account) {

		return this.accDao.getUser(account.getLogin());
	}

	@Override
	public int updateAccount(AccountingTBL account) {

		AccountingTBL tmpAcc_1 = this.accDao.findAccountByLogin(account
				.getLogin());

		tmpAcc_1.setLogin(account.getLogin());
		tmpAcc_1.setPassword(account.getPassword());
		tmpAcc_1.setAdmin(account.isAdmin());
		tmpAcc_1.setRole_BillsTAB(account.isRole_BillsTAB());
		tmpAcc_1.setRole_CustomerTAB(account.isRole_CustomerTAB());
		tmpAcc_1.setRole_CalendarTAB(account.isRole_CalendarTAB());
		tmpAcc_1.setRole_DeliveriesTAB(account.isRole_DeliveriesTAB());
		tmpAcc_1.setRole_DoctorLetterTAB(account.isRole_DoctorLetterTAB());
		tmpAcc_1.setRole_DoctorsDB(account.isRole_DoctorsDB());
		tmpAcc_1.setRole_GravTestTAB(account.isRole_GravTestTAB());
		tmpAcc_1.setRole_InternAnaTAB(account.isRole_InternAnaTAB());
		tmpAcc_1.setRole_LaborbookTAB(account.isRole_LaborbookTAB());
		tmpAcc_1.setRole_OPBillsTAB(account.isRole_OPBillsTAB());
		tmpAcc_1.setRole_OrdonanceTAB(account.isRole_OrdonanceTAB());
		tmpAcc_1.setRole_RemindersTAB(account.isRole_RemindersTAB());
		tmpAcc_1.setRole_tarife(account.isRole_tarife());
		tmpAcc_1.setRole_ZytologyTAB(account.isRole_ZytologyTAB());
		tmpAcc_1.setRole_ZytoZettelTAB(account.isRole_ZytoZettelTAB());

		this.accDao.update(tmpAcc_1);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void logInUser(AccountingTBL userAccount) {

		// Get User account
		AccountingTBL user = this.getAccountByLogin(userAccount);

		if (user != null) {
			user.setOnline(new Boolean(true));
			user.setLastLogInTime(new GregorianCalendar());
			this.accDao.update(user);
		}

	}

	@Override
	public void logOutUser(AccountingTBL userAccount) {

		// Get User account
		AccountingTBL user = this.accDao.findAccountByLogin(userAccount.getLogin());

		if (user != null) {
			user.setOnline(new Boolean(false));
			this.accDao.update(user);
		}

	}

}
